package com.kehutong.charge.service.impl;

import com.kehutong.charge.dao.ChargePenaltyDao;
import com.kehutong.charge.dto.ChargePenaltyOrderDTO;
import com.kehutong.charge.util.JDBCUtil;
import com.kehutong.charge.util.enums.ChargeBillStatusEnum;
import com.kehutong.charge.vo.ChargePenaltyOrderExcelVO;
import com.kehutong.charge.vo.ChargePenaltyOrderHouseExcelVO;
import com.kehutong.charge.entity.ChargePenaltyManage;
import com.kehutong.charge.entity.ChargePenaltyOrder;
import com.kehutong.charge.service.ChargePenaltyService;
import com.kehutong.common.errcode.Assert;
import com.kehutong.common.excel.tool.ExcelTool;
import com.kehutong.common.util.ListUtils;
import org.coraframework.converter.util.StringUtil;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.Condition;
import org.coraframework.orm.jdbc.execute.PageCondition;
import org.coraframework.orm.jdbc.execute.QueryCondition;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author joneRon
 * @Title: kht
 * @Description: 违约金服务接口
 * @date 2021/7/27
 */
public class ChargePenaltyServiceImpl implements ChargePenaltyService {

    @Inject
    private JdbcSession jdbcSession;
    @Inject
    private ChargePenaltyDao chargePenaltyDao;
    @Inject
    private ExcelTool excelTool;



    @Override
    public Page<ChargePenaltyOrder> findPage(JSONObject jsonObject) {
        FindPage<ChargePenaltyOrder> find = jdbcSession.findPage(ChargePenaltyOrder.class)
                .eq("deleted", false)
                .eq(jsonObject,"communityNo")
                .eq(jsonObject,"houseNo")
                .eq(jsonObject,"orderNo")
                .eq("billStatus", ChargeBillStatusEnum.BILL_WAIT_PAID);

        // 处理时间
        JDBCUtil.between(jsonObject, find, "penaltyDate");

        //添加选中导出条件
        JSONArray exportIdList = jsonObject.getJSONArray("exportIdList");
        if(!Objects.isEmpty(exportIdList)){
            find.in("penaltyOrderNo",exportIdList);
        }
        if (StringUtil.isNotBlank(jsonObject.getString("search"))) {
            find.markBegin()
                    .like("penaltyNo", jsonObject.get("search"))
                    .or()
                    .like("itemBillNo", jsonObject.get("search"))
                    .or()
                    .like("userName", jsonObject.get("search"))
                    .or()
                    .like("chargeAddress", jsonObject.getString("search"))
                    .markEnd();
        }
        find.order("createTime", "desc");

        return find.page(jsonObject).exe();
    }


    @Override
    public void pageOrderExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject) {
        jsonObject.put("pageNo", 1);
        jsonObject.put("pageSize", Short.MAX_VALUE);
        List<ChargePenaltyOrder> list = findPage(jsonObject).getList();
        Assert.notEmpty(list,"导出失败！没有找到相关的违约金订单数据");
        List<ChargePenaltyOrderExcelVO> dtoList = ListUtils.copyList(list, ChargePenaltyOrderExcelVO::new);
        excelTool.excelDownload(req,resp,dtoList,"违约金订单列表导出");
    }


    @Override
    public List<ChargePenaltyOrder> findList(JSONObject jsonObject) {
        return jdbcSession.findArray(ChargePenaltyOrder.class)
                .eq("deleted", false)
                .eq(jsonObject,"houseNo")
                .eq(jsonObject,"penaltyOrderNo")
                .exe();
    }


    @Override
    public Object save(ChargePenaltyOrder chargePenaltyOrder) throws Exception {
        return jdbcSession.insert(chargePenaltyOrder);
    }


    @Override
    public Object update(ChargePenaltyOrder chargePenaltyOrder) throws Exception {
        return jdbcSession.updateById(chargePenaltyOrder);
    }


    @Override
    public Object deleteById(String id) {
        return null;
    }


    @Override
    public Page<ChargePenaltyOrderDTO> findStatistPage(JSONObject jsonObject) {
        PageCondition query = Condition.createPage(false);

        String communityNo = jsonObject.getString("communityNo");
        if(!StringUtil.isEmpty(communityNo)){
            query.eq("communityNo",communityNo);
        }
        query.eq("deleted", false);
        query.groupBy("communityNo");
        query.order("createTime","desc");

        return chargePenaltyDao.statistPage(query);
    }


    @Override
    public void statistPageExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject) {
        jsonObject.put("pageNo", 1);
        jsonObject.put("pageSize", Short.MAX_VALUE);
        List<ChargePenaltyOrderDTO> list = findStatistPage(jsonObject).getList();
        Assert.notEmpty(list,"导出失败！没有找到相关的违约金统计数据");
        List<ChargePenaltyOrderDTO> dtoList = ListUtils.copyBeanList(list, ChargePenaltyOrderDTO::new);
        excelTool.excelDownload(req,resp,dtoList,"违约金统计导出");
    }


    @Override
    public Page<ChargePenaltyOrderDTO> housePage(JSONObject jsonObject) {
        PageCondition query = Condition.createPage(false);

        String communityNo = jsonObject.getString("communityNo");
        String typeName = jsonObject.getString("typeName");
        if(!StringUtil.isEmpty(typeName)){
            query.like("userName",typeName);
        }
        query.eq("communityNo",communityNo);
        query.eq("deleted", false);
        query.groupBy("houseNo");
        query.order("createTime","desc");

        return chargePenaltyDao.statistPage(query);
    }


    @Override
    public void housePageExport(HttpServletRequest req, HttpServletResponse resp, JSONObject jsonObject) {
        jsonObject.put("pageNo", 1);
        jsonObject.put("pageSize", Short.MAX_VALUE);
        List<ChargePenaltyOrderDTO> list = housePage(jsonObject).getList();
        Assert.notEmpty(list,"导出失败！没有找到相关的小区违约金用户数据");
        List<ChargePenaltyOrderHouseExcelVO> dtoList = ListUtils.copyBeanList(list, ChargePenaltyOrderHouseExcelVO::new);
        excelTool.excelDownload(req,resp,dtoList,"小区违约金用户统计导出");
    }


    @Override
    public Object houseDetailPage(JSONObject jsonObject) {
        FindPage<ChargePenaltyOrder> find = jdbcSession.findPage(ChargePenaltyOrder.class)
                .eq("deleted", false)
                .eq(jsonObject, "houseNo")
                .order("createTime", "desc");

        return find.page(jsonObject).exe();
    }


    @Override
    public Object findManageInfo(JSONObject jsonObject) {

        return jdbcSession.findById(ChargePenaltyManage.class,jsonObject.getString("id"));
    }

    @Override
    public Object findStatist(JSONObject jsonObject) {

        QueryCondition query = Condition.createQuery(false);
        query.eq("deleted", false);
        query.eq(jsonObject,"houseNo");
        query.groupBy("houseNo");

        return chargePenaltyDao.findStatist(query);
    }


}
